package btree4j.template;

import btree4j.Value;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Template implements Serializable {
    private static final long serialVersionUID = 1L;

    private long id;
    private String name;
    private List<Column> columns;
    private List<String> indexedColumns = new ArrayList<>(); // 维护索引列

    public Template() {
        this.columns = new ArrayList<>();
    }

    public Template(String name) {
        this();
        this.name = name;
    }

    public void addIndexedColumn(String columnName) {
        if (!indexedColumns.contains(columnName)) {
            indexedColumns.add(columnName);
        }
    }

    public List<String> getIndexedColumns() {
        return indexedColumns;
    }

    public long getId() {
        return id;
    }
    

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void addColumn(Column column) {
        columns.add(column);
        if (column.isIndexed()) {
            addIndexedColumn(column.getName());
        }
    }

    public Column getColumn(int index) {
        return columns.get(index);
    }

    public int getColumnCount() {
        return columns.size();
    }

    public Column getColumnByName(String columnName) {
        for (Column column : columns) {
            if (column.getName().equals(columnName)) {
                return column;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Template [id=").append(id)
                .append(", name=").append(name)
                .append(", columns=").append(columns)
                .append(", indexedColumns=").append(indexedColumns)
                .append("]");
        return sb.toString();
    }
}